package com.briup.pts.utils;

import java.security.SecureRandom;

/**
 * @BelongsProject: briup-pts
 * @BelongsPackage: com.briup.pts.utils
 * @Author: 鲨鲨天下第一
 * @CreateTime: 2024-11-01  21:02
 * @Description: TODO
 * @Version: 1.0
 */
public class RandomStringGenerator {
    // 字符池，可以包含字母、数字和特殊字符
//    private static final String CHAR_POOL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+[]{}|;:'\",.<>/?";
    private static final String CHAR_POOL = "012345678901234567890123456789abcdefghijklmnopqrstuvwxyz012345678901234567890123456789";

    // SecureRandom 提供了更强的随机性
    private static final SecureRandom RANDOM = new SecureRandom();

    /**
     * 生成指定长度的随机字符串
     *
     * @param length 要生成的随机字符串的长度
     * @return 随机字符串
     */
    public static String generateRandomString(int length) {
        StringBuilder randomString = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = RANDOM.nextInt(CHAR_POOL.length());
            randomString.append(CHAR_POOL.charAt(randomIndex));
        }
        return randomString.toString();
    }
}
